Seaborn is an open-source Python library for data visualization built-on Matplotlib.
!conda install -c anaconda seaborn -y
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import numpy as np
from matplotlib import pyplot as plt
def sin_plot():
x = np.linspace(-10, 10, 100)
for i in range(1, 5):
plt.plot(x, np.sin(x - i))
sin_plot()
plt.show()
matplotlib with seaborn¶import numpy as np
from matplotlib import pyplot as plt
import seaborn as sns
sns.set()
def sin_plot():
x = np.linspace(-10, 10, 100)
for i in range(1, 5):
plt.plot(x, np.sin(x - i))
sin_plot()
plt.show()
seaborn¶import numpy as np
from matplotlib import pyplot as plt
import seaborn as sns
# white, dark, whitegrid, darkgrid, ticks
sns.set_style('darkgrid')
def sin_plot():
x = np.linspace(-10, 10, 100)
for i in range(1, 5):
plt.plot(x, np.sin(x - i))
sin_plot()
plt.show()
matplotlib and seaborn can be used together¶x = np.linspace(-10, 10, 100)
data = pd.DataFrame(data={
'x': x,
'sin_x': np.sin(x)
})
sns.set()
# seaborn with plt.title()
sns.lineplot(x='x', y='sin_x', data=data)
plt.title('x and sin(x)')
plt.show()
# seaborn with plt.xlim() and plt.ylim()
sns.lineplot(x='x', y='sin_x', data=data)
plt.xlim(0, 10)
plt.ylim(0, 1)
plt.show()
# seaborn with plt.subplot()
for subplot_index in range(6):
plt.subplot(3, 2, subplot_index+1)
sns.lineplot(x='x', y='sin_x', data=data)
plt.tight_layout()
plt.show()
sns.get_dataset_names()
['anagrams', 'anscombe', 'attention', 'brain_networks', 'car_crashes', 'diamonds', 'dots', 'exercise', 'flights', 'fmri', 'gammas', 'geyser', 'iris', 'mpg', 'penguins', 'planets', 'taxis', 'tips', 'titanic']
data_iris = sns.load_dataset('iris')
data_iris
| sepal_length | sepal_width | petal_length | petal_width | species | |
|---|---|---|---|---|---|
| 0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
| 1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
| 2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
| 3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
| 4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
| ... | ... | ... | ... | ... | ... |
| 145 | 6.7 | 3.0 | 5.2 | 2.3 | virginica |
| 146 | 6.3 | 2.5 | 5.0 | 1.9 | virginica |
| 147 | 6.5 | 3.0 | 5.2 | 2.0 | virginica |
| 148 | 6.2 | 3.4 | 5.4 | 2.3 | virginica |
| 149 | 5.9 | 3.0 | 5.1 | 1.8 | virginica |
150 rows × 5 columns
data_titanic = sns.load_dataset('titanic')
data_titanic
| survived | pclass | sex | age | sibsp | parch | fare | embarked | class | who | adult_male | deck | embark_town | alive | alone | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 3 | male | 22.0 | 1 | 0 | 7.2500 | S | Third | man | True | NaN | Southampton | no | False |
| 1 | 1 | 1 | female | 38.0 | 1 | 0 | 71.2833 | C | First | woman | False | C | Cherbourg | yes | False |
| 2 | 1 | 3 | female | 26.0 | 0 | 0 | 7.9250 | S | Third | woman | False | NaN | Southampton | yes | True |
| 3 | 1 | 1 | female | 35.0 | 1 | 0 | 53.1000 | S | First | woman | False | C | Southampton | yes | False |
| 4 | 0 | 3 | male | 35.0 | 0 | 0 | 8.0500 | S | Third | man | True | NaN | Southampton | no | True |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 886 | 0 | 2 | male | 27.0 | 0 | 0 | 13.0000 | S | Second | man | True | NaN | Southampton | no | True |
| 887 | 1 | 1 | female | 19.0 | 0 | 0 | 30.0000 | S | First | woman | False | B | Southampton | yes | True |
| 888 | 0 | 3 | female | NaN | 1 | 2 | 23.4500 | S | Third | woman | False | NaN | Southampton | no | False |
| 889 | 1 | 1 | male | 26.0 | 0 | 0 | 30.0000 | C | First | man | True | C | Cherbourg | yes | True |
| 890 | 0 | 3 | male | 32.0 | 0 | 0 | 7.7500 | Q | Third | man | True | NaN | Queenstown | no | True |
891 rows × 15 columns
sns.lineplot(x='sepal_length', y='sepal_width', data=data_iris)
plt.show()
sns.lineplot(x='sepal_length', y='sepal_width', hue='species', data=data_iris)
plt.show()
sns.scatterplot(x='petal_length', y='petal_width', data=data_iris)
plt.show()
sns.scatterplot(x='petal_length', y='petal_width', data=data_iris, marker='*')
plt.show()
sns.scatterplot(x='petal_length', y='petal_width', hue='species', data=data_iris)
plt.show()
sns.jointplot(x='petal_length', y='petal_width', data=data_iris)
plt.show()
sns.jointplot(x='petal_length', y='petal_width', hue='species', data=data_iris)
plt.show()
sns.regplot(x='sepal_length', y='petal_length', data=data_iris)
plt.show()
sns.jointplot(x='petal_length', y='petal_width', data=data_iris, kind='reg')
plt.show()
data_titanic = sns.load_dataset('titanic')
data_titanic
| survived | pclass | sex | age | sibsp | parch | fare | embarked | class | who | adult_male | deck | embark_town | alive | alone | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 3 | male | 22.0 | 1 | 0 | 7.2500 | S | Third | man | True | NaN | Southampton | no | False |
| 1 | 1 | 1 | female | 38.0 | 1 | 0 | 71.2833 | C | First | woman | False | C | Cherbourg | yes | False |
| 2 | 1 | 3 | female | 26.0 | 0 | 0 | 7.9250 | S | Third | woman | False | NaN | Southampton | yes | True |
| 3 | 1 | 1 | female | 35.0 | 1 | 0 | 53.1000 | S | First | woman | False | C | Southampton | yes | False |
| 4 | 0 | 3 | male | 35.0 | 0 | 0 | 8.0500 | S | Third | man | True | NaN | Southampton | no | True |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 886 | 0 | 2 | male | 27.0 | 0 | 0 | 13.0000 | S | Second | man | True | NaN | Southampton | no | True |
| 887 | 1 | 1 | female | 19.0 | 0 | 0 | 30.0000 | S | First | woman | False | B | Southampton | yes | True |
| 888 | 0 | 3 | female | NaN | 1 | 2 | 23.4500 | S | Third | woman | False | NaN | Southampton | no | False |
| 889 | 1 | 1 | male | 26.0 | 0 | 0 | 30.0000 | C | First | man | True | C | Cherbourg | yes | True |
| 890 | 0 | 3 | male | 32.0 | 0 | 0 | 7.7500 | Q | Third | man | True | NaN | Queenstown | no | True |
891 rows × 15 columns
sns.barplot(x='sex', y='survived', data=data_titanic)
plt.show()
sns.barplot(x='sex', y='survived', hue='class', data=data_titanic)
plt.show()
# kde=False
sns.histplot(x='petal_length', kde=False, data=data_iris)
plt.show()
# kde=True
sns.histplot(x='petal_length', kde=True, data=data_iris)
plt.show()
# kde=True
x = np.random.normal(100, size=100000)
sns.histplot(x, kde=True, bins=100)
plt.show()
hue¶sns.histplot(x='petal_length', kde=True, data=data_iris)
plt.show()
sns.histplot(x='petal_length', hue='species', kde=True, data=data_iris)
plt.show()
sns.countplot(x='sex', data=data_titanic)
plt.show()
sns.countplot(x='sex', hue='class', data=data_titanic)
plt.show()
sns.countplot(y='sex', hue='class', data=data_titanic)
plt.show()
sns.boxplot(x='sex', y='age', data=data_titanic)
plt.show()
sns.boxplot(x='sex', y='age', hue='class', data=data_titanic)
plt.show()
meanprops = {
'marker': '+',
'markeredgecolor': 'white',
'markersize': '10'
}
sns.boxplot(x='sex', y='age', data=data_titanic, showmeans=True, meanprops=meanprops)
plt.show()
# Box plot
sns.boxplot(x='class', y='age', data=data_titanic)
plt.show()
# Violin plot
sns.violinplot(x='class', y='age', data=data_titanic)
plt.show()
# Box plot
sns.boxplot(x='class', y='age', hue='sex', data=data_titanic)
plt.show()
# Violin plot
sns.violinplot(x='class', y='age', hue='sex', data=data_titanic)
plt.show()
# Violin plot with split=True
sns.violinplot(x='class', y='age', hue='sex', data=data_titanic, split=True)
plt.show()
split=True¶sns.boxplot(x='sex', y='age', hue='class', data=data_titanic)
plt.show()
sns.violinplot(x='sex', y='age', hue='class', data=data_titanic)
plt.show()
sns.violinplot(x='sex', y='age', hue='class', data=data_titanic, split=True)
plt.show()
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) /var/folders/9m/8kxkp3s10g9gnjlz2bk_8zs00000gp/T/ipykernel_2751/921161436.py in <module> ----> 1 sns.violinplot(x='sex', y='age', hue='class', data=data_titanic, split=True) 2 plt.show() ~/opt/anaconda3/envs/work_env/lib/python3.8/site-packages/seaborn/_decorators.py in inner_f(*args, **kwargs) 44 ) 45 kwargs.update({k: arg for k, arg in zip(sig.parameters, args)}) ---> 46 return f(**kwargs) 47 return inner_f 48 ~/opt/anaconda3/envs/work_env/lib/python3.8/site-packages/seaborn/categorical.py in violinplot(x, y, hue, data, order, hue_order, bw, cut, scale, scale_hue, gridsize, width, inner, split, dodge, orient, linewidth, color, palette, saturation, ax, **kwargs) 2385 ): 2386 -> 2387 plotter = _ViolinPlotter(x, y, hue, data, order, hue_order, 2388 bw, cut, scale, scale_hue, gridsize, 2389 width, inner, split, dodge, orient, linewidth, ~/opt/anaconda3/envs/work_env/lib/python3.8/site-packages/seaborn/categorical.py in __init__(self, x, y, hue, data, order, hue_order, bw, cut, scale, scale_hue, gridsize, width, inner, split, dodge, orient, linewidth, color, palette, saturation) 539 if split and self.hue_names is not None and len(self.hue_names) != 2: 540 msg = "There must be exactly two hue levels to use `split`.'" --> 541 raise ValueError(msg) 542 self.split = split 543 ValueError: There must be exactly two hue levels to use `split`.'
sns.stripplot(x='sex', y='age', data=data_titanic)
plt.show()
sns.stripplot(x='sex', y='age', hue='class', data=data_titanic)
plt.show()
Use to visualize pairwise relationship
data_iris
| sepal_length | sepal_width | petal_length | petal_width | species | |
|---|---|---|---|---|---|
| 0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
| 1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
| 2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
| 3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
| 4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
| ... | ... | ... | ... | ... | ... |
| 145 | 6.7 | 3.0 | 5.2 | 2.3 | virginica |
| 146 | 6.3 | 2.5 | 5.0 | 1.9 | virginica |
| 147 | 6.5 | 3.0 | 5.2 | 2.0 | virginica |
| 148 | 6.2 | 3.4 | 5.4 | 2.3 | virginica |
| 149 | 5.9 | 3.0 | 5.1 | 1.8 | virginica |
150 rows × 5 columns
sns.pairplot(data_iris,
hue='species',
diag_kind='kde',
kind='scatter',
palette='husl')
plt.show()
data_heatmap = np.random.randint(0, 100, (5, 5))
data_heatmap
array([[77, 44, 3, 18, 30],
[49, 78, 83, 78, 94],
[99, 42, 0, 4, 38],
[50, 41, 66, 51, 71],
[44, 30, 86, 66, 40]])
sns.heatmap(data_heatmap, annot=True)
plt.show()